package com.das.metrocloud.ssoserver.controller;

import com.das.framework.common.annotation.ActionPermission;
import com.das.framework.common.annotation.MethodPermission;
import com.das.framework.common.extvo.ResultVO;
import com.das.framework.common.extvo.TokenVo;
import com.das.framework.common.redis.utils.RedisKeyUtils;
import com.das.framework.common.redis.utils.RedisUtils;
import com.das.framework.common.utils.Tools;
import com.das.framework.common.vo.PageVO;
import com.das.framework.web.spring.controller.SupportController;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;



/**
 *
 * 带状态的 Token, 可以让 Token 失效,   Token是保存在Redis中
 *
 *
 */
@RestController
@RequestMapping({"/auth/online"})
@ActionPermission(
        code = "sso_online",
        description = "用户认证管理"
)
public class OnlineUserController extends SupportController {

    @Value("${dascloud.token.expire:1800}")
    private long expire;


    @Autowired
    private RedisUtils redisUtils;



    public OnlineUserController() {}

    @ResponseBody
    @RequestMapping(
            value = {"/query", "/query/{pageSize}", "/query/{pageSize}/{pageNo}"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    @MethodPermission(
            codes = {"SEARCH"},
            description = "查询认证信息"
    )
    public PageVO queryOnlineUser(@PathVariable(value = "pageSize",required = false) Integer pageSize, @PathVariable(value = "pageNo",required = false) Integer pageNo) {

        PageVO pageVO = new PageVO(pageNo, pageSize);

        List<TokenVo> list = new ArrayList();

        Set<?> set = (Set)this.redisUtils.gets("TK:*");

        Iterator var6 = set.iterator();

        while(var6.hasNext()) {
            Object object = var6.next();
            Object obj = this.redisUtils.get(object.toString());
            if (obj != null) {
                list.add((TokenVo)obj);
            }
        }

        pageVO.setResults(list);
        pageVO.setTotalCount(list.size());
        return pageVO;
    }

    @RequestMapping(
            value = {"/changeStatus"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    @MethodPermission(
            codes = {"UPDATE"},
            description = "修改认证状态"
    )
    public ResultVO changeStatus(@RequestBody TokenVo entity) {
        try {
            if (Tools.StringIsNullOrSpace(entity.getUserId())) {
                return new ResultVO(false, "请求参数不完整.");
            } else {
                String tokenId = RedisKeyUtils.base642tokenId(entity.getUserId());
                Object obj = this.redisUtils.get(tokenId);
                if (obj == null) {
                    return new ResultVO(false, "该认证信息不存在或用户已下线.");
                } else {
                    TokenVo token = (TokenVo)obj;
                    token.setStatus(entity.getStatus());
                    this.redisUtils.set(tokenId, token, this.expire);
                    return new ResultVO(true, "修改用户认证状态成功");
                }
            }
        } catch (Exception var5) {
            log.error(var5.getMessage(), var5);
            return new ResultVO(false, "修改用户认证状态时发生错误:" + var5.getMessage(), "500");
        }
    }
}
